Skip to content

tikv, txnkv: unify the way background goroutines are spawned - #2064

Draft
ljluestc wants to merge 1 commit into
tikv:masterfrom
ljluestc:unify-background-job-spawning
Draft

ljluestc wants to merge 1 commit into
tikv:masterfrom
ljluestc:unify-background-job-spawning

Conversation

@ljluestc

Copy link
Copy Markdown

Background jobs used to be started in three different ways: a builtin go statement, KVStore.Go, and KVTxn.spawnWithStorePool. Each of them tracked (or failed to track) the goroutine differently, which is the kind of mismatch that caused pingcap/tidb#57213.

Make KVStore.Go the single entry point. It now registers the job with the store WaitGroup, runs it in the store pool, and rejects new jobs once the store starts closing, so Close waits for background jobs instead of racing with them. KVTxn.spawn and KVTxn.spawnWithStorePool collapse into one spawn that goes through it.

Along the way:

  • Spool.Run returns ErrPoolClosed instead of letting the underlying pool silently drop the function, so a rejected job is no longer indistinguishable from an accepted one. Spool.Close is now idempotent.
  • KVTxn.spawn calls the Post lifecycle hook when the goroutine is rejected, which keeps it balanced with the Pre hook that already ran.
  • Callers that prepared state for a background job (cleanWg, the async pessimistic rollback WaitGroup, the broadcast rate limit) release it when the job is rejected, instead of leaving a waiter blocked forever.
  • WaitGroup() is dropped from the kvstore interface used by the transaction package and deprecated on KVStore.

Foreground fan-out goroutines that the caller joins before returning, such as batchExecutor.startWorker and KVStore.updateMinSafeTS, are left as plain go statements: they are not background jobs and do not outlive their caller.

Closes #1510

Background jobs used to be started in three different ways: a builtin `go`
statement, `KVStore.Go`, and `KVTxn.spawnWithStorePool`. Each of them tracked
(or failed to track) the goroutine differently, which is the kind of mismatch
that caused pingcap/tidb#57213.

Make `KVStore.Go` the single entry point. It now registers the job with the
store WaitGroup, runs it in the store pool, and rejects new jobs once the store
starts closing, so `Close` waits for background jobs instead of racing with
them. `KVTxn.spawn` and `KVTxn.spawnWithStorePool` collapse into one `spawn`
that goes through it.

Along the way:

- `Spool.Run` returns `ErrPoolClosed` instead of letting the underlying pool
  silently drop the function, so a rejected job is no longer indistinguishable
  from an accepted one. `Spool.Close` is now idempotent.
- `KVTxn.spawn` calls the `Post` lifecycle hook when the goroutine is rejected,
  which keeps it balanced with the `Pre` hook that already ran.
- Callers that prepared state for a background job (`cleanWg`, the async
  pessimistic rollback WaitGroup, the broadcast rate limit) release it when the
  job is rejected, instead of leaving a waiter blocked forever.
- `WaitGroup()` is dropped from the `kvstore` interface used by the transaction
  package and deprecated on `KVStore`.

Foreground fan-out goroutines that the caller joins before returning, such as
`batchExecutor.startWorker` and `KVStore.updateMinSafeTS`, are left as plain
`go` statements: they are not background jobs and do not outlive their caller.

Closes tikv#1510

Signed-off-by: ljluestc <63439129+ljluestc@users.noreply.github.com>
@ti-chi-bot ti-chi-bot Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates the PR's author has signed the dco. labels Sep 13, 2026
@ti-chi-bot

ti-chi-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign cfzjywxk for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added contribution This PR is from a community contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Sep 13, 2026
@ti-chi-bot

ti-chi-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

Hi @ljluestc. Thanks for your PR.

I'm waiting for a tikv member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot Bot added the first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. label Sep 13, 2026
@ti-chi-bot

ti-chi-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

Welcome @ljluestc!

It looks like this is your first PR to tikv/client-go 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to tikv/client-go. 😃

@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ti-chi-bot ti-chi-bot Bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify the approaches of spawning background jobs

1 participant